﻿Imports System.Data.OleDb
Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_addcomment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addcomment.Click
        Dim sql As String = "INSERT INTO Comments (Author,Comment,Title,Island_fkID) VALUES (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("IslandConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_comment.Text)
        cmd.Parameters.AddWithValue("@f3", tb_title.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btn_at_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_at.Click
        Dim sql As String = "INSERT INTO IslandTags (Island_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("IslandConnectionString").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cb_tag.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
    End Sub

    Protected Sub btn_nt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_nt.Click
        Dim sql As String = "INSERT INTO Tag (TagName) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("IslandConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_nt.Text)
        cmd.ExecuteNonQuery()
        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tag ORDER BY ID Desc", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)

        Dim associate As String = "INSERT INTO IslandTags (Island_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()
    End Sub
End Class